<?php
include_once('DataBase.php');
/*
 * Class designed to model the database table 'Book'
 */
class Book{
    //Attributes
    
    //Denotes the unique identifier of the book
    private $quote;
    //Name of the book
    private $name;
    //Author of the book
    private $author;
    //House that edited the book
    private $editorial;
    
    //Methods
    
    //Builds an object with the information specified
    public function __construct($quote, $name, $author, $editorial){
        $this->quote = $quote;
        $this ->name = $name;
        $this ->author = $author;
        $this ->editorial = $editorial;
    }
 
    /*
     * Find in the database a specified book using its quote
     *
     * Parameters: $quote -> Unique identifier for the book
     * Return value: A Book object containing the information of the desired
     *               book or null if it was not found.
     */
     public static function findBook($quote){
        $database = DataBase::getInstance();
        return $database->selectBook($quote);        
     }
  
    /*
     * Returns an array containing all the available books of the data base
     *
     * Return value: An array of Book containing all available books
     */
    public static function getAllAvailable(){
        $database = DataBase::getInstance();
        return $database->getAllAvailableBooks();
    }
    
    /*
     * Boolean function that verifies if the book instance is available for rent
     *
     * Return value: True if the book is available, false otherwise
     */
     public function isAvailable(){
        $allAvailable = Book::getAllAvailable();
        foreach($allAvailable as $book){
            if($book->getQuote() == $this->getQuote())
                return true;
        }
        
        return false;
     }
     
     /*
      * Deletes the loan corresponding to this book
      */
      public function returnBook(){
        $database = DataBase::getInstance();
        $database->deleteLoan($this->getQuote());
      }
    //List of getters
    public function getQuote(){return $this->quote;}
    public function getName(){return $this->name;}
    public function getAuthor(){return $this->author;}
    public function getEditorial(){return $this->editorial;}
    
    //List of setters
    public function setQuote($a){$this->quote = $a;}
    public function setName($a){$this->name = $a;}
    public function setAuthor($a){$this->author = $a;}
    public function setEditorial($a){$this->editorial = $a;}
}

?>